<?php

	require_once(dirname(dirname(dirname(dirname(__FILE__)))) . "/engine/start.php");
	gatekeeper();
	
	$friend = get_entity(get_input("friend"));
	if($friend){
		$current_user = get_loggedin_user();
		$session = get_entity(get_input("chatsession"));
		$maxmembers = get_entities_from_relationship("elggchat_session_member", $session->guid, null, null, null, null, null, null, null, true);
		$members = get_entities_from_relationship("elggchat_session_member", $session->guid, null, null, null, null, null, $maxmembers);
		if(is_member(get_loggedin_userid(), $members)){
			add_entity_relationship( $session->guid,"elggchat_session_member_invite", $friend->guid); 
			$message = sprintf(elgg_echo('elggchat:action:invite'), $current_user->name, $friend->name);
			$session->annotate("elggchat_system_message", $message,ACCESS_LOGGED_IN,$current_user->guid);
		}
	}
	exit();
?>
